﻿using UnityEngine;
using System.Collections;

public class MovingAverage {

    private int size = 25;
    private float[] AverageBuffer;
    private int point = 0;
    // Use this for initialization
    public MovingAverage() {
        Create();
   }
    public MovingAverage(int bufferSize)
    {
        this.size = bufferSize;
        Create();
    }

    private void Create()
    {
        AverageBuffer = new float[size];
    }
    public void Clear()
    {
        for(int i=0;i<size;i++)
        {
            AverageBuffer[i] = 0;
        }
    }
    public float SmoothValue(float inputValue)
    {
        AverageBuffer[point] = inputValue;
        float v = 0.0f;
        for (int i = 0; i < size; i++)
        {
            v = v + AverageBuffer[i];
        }
        v = v / (float)size;

        MovePoint();
        return v;
    }
     public float  GetPreValue()
    {
        int p = point -1;
        if (p <0)
        {
            p = size - 1;
        }
        return AverageBuffer[ p];
    }
    // to move point circular 
    private void MovePoint()
    {
        point++;
        if (point >= size)
        {
            point = 0;
        }
    }


}
